<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>computed</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>

</head>
<body>
<div id="app">
    num={{g_num}}
    <counter v-on:plus="gNumPlus" v-on:reduce="gNumReduce" v-bind:num="g_num"></counter>
</div>

<script type="text/javascript">

    //组件
    const counter = {
        template: `
        <div>
            <button v-on:click="numPlus">增加</button>
            <button v-on:click="numReduce">减少</button>
        </div>
       `,
        data() {
            return {
                num: 1
            }
        },
        methods: {
            numPlus() {
                console.log(this.num)
                this.$emit("plus")
            },
            numReduce() {
                console.log(this.num)
                this.$emit("reduce")
            }
        }
    }
    Vue.component("counter", counter);


    var app = new Vue({
        el: "#app",
        data: {
            g_num: 1
        },
        methods: {
            gNumPlus() {
                this.g_num++;

            },
            gNumReduce() {
                this.g_num--;
            }
        }


    })
</script>
</body>
</html>